package com.kumaran.calci2;

import com.google.ads.AdRequest;
import com.google.ads.AdView;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class Calculator extends Activity {
	EditText FirstValue, SecondValue;
	TextView Result;
	Button Multiply, Divide, Add, Sub, Close;
	float num1 , num2;
	//private AdView adView;
	//private String a14ef304ca66de6;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);




		FirstValue = (EditText) findViewById(R.id.EditText01);
		SecondValue = (EditText) findViewById(R.id.EditText02);
		Result = (TextView) findViewById(R.id.TextView01);
		Result.setText("0.00");
		Multiply = (Button) findViewById(R.id.Multiply);
		Divide = (Button)findViewById(R.id.Divide);
		Add = (Button)findViewById(R.id.Add);
		Sub = (Button)findViewById(R.id.Sub);
		Close = (Button)findViewById(R.id.Close) ;

		AdView adView = (AdView)findViewById(R.id.adView);
		AdRequest re = new AdRequest();
		re.setGender(AdRequest.Gender.MALE);
		re.addKeyword("kumaran");
		adView.loadAd(re);

		Multiply.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Getting first & second values and passing to show result
				String first=FirstValue.getText().toString().trim();
				String second=SecondValue.getText().toString().trim();

				if(first.length()>0 && second.length()>0){

					float num1 = Float.parseFloat(first);
					float num2 = Float.parseFloat(second);

					showMultiply(num1, num2);
				}
				else{
					showToast();	

				}

			}


		});

		Divide.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Getting first & second values and passing to show result
				String first=FirstValue.getText().toString().trim();
				String second=SecondValue.getText().toString().trim();

				if(first.length()>0 && second.length()>0){

					float num1 = Float.parseFloat(first);
					float num2 = Float.parseFloat(second);

					showDivide(num1, num2);
				}
				else{
					showToast();
				}
			}
		});

		Add.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Getting first & second values and passing to show result
				String first=FirstValue.getText().toString().trim();
				String second=SecondValue.getText().toString().trim();

				if(first.length()>0 && second.length()>0){

					float num1 = Float.parseFloat(first);
					float num2 = Float.parseFloat(second);

					showAdd(num1, num2);
				}
				else{
					showToast();
				}

			}
		});

		Sub.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Getting first & second values and passing to show result
				String first=FirstValue.getText().toString().trim();
				String second=SecondValue.getText().toString().trim();

				if(first.length()>0 && second.length()>0){

					float num1 = Float.parseFloat(first);
					float num2 = Float.parseFloat(second);

					showSub(num1, num2);
				}
				else{
					showToast();
				}


			}
		});
		Close.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});


		//IMAdRequest adRequest = new IMAdRequest();
		//imAdView.setIMAdRequest(adRequest);
		//imAdView.loadNewAd();

		//MobclixAdView adview = new MobclixMMABannerXLAdView(this);

		//ViewGroup parentView = null;
		//parentView.addView(adview);

		//AdWhirlLayout mAdWhirlLayout = (AdWhirlLayout) findViewById(R.id.adwhirl_layout);
		//This is an important step in order to use the custom event created.
		//mAdWhirlLayout.setAdWhirlInterface(new CustomEvents(mAdWhirlLayout, this,
		//getApplicationContext()));

		//AdView adView = (AdView)this.findViewById(R.id.banner);
		//adView.loadAd(new AdRequest());

		//WebView adsDisplay = (WebView) findViewById(R.id.adsDisplay);
		//adsDisplay.getSettings().setJavaScriptEnabled(true);
		//adsDisplay.loadUrl("http://satheesh.site90.net/index.html");
		//String publisherId = "f1644ebbd7522b2b76fc61487291db68";
		//boolean includeLocation = false;
		//Mode mode = Mode.TEST;
		//boolean animation = true;
		//MobFoxView mobfoxView = new MobFoxView(this, publisherId, test, includeLocation, animation);

		//mAdView = (MoPubView) findViewById(R.id.adview);
		// mAdView.setAdUnitId("agltb3B1Yi1pbmNyDQsSBFNpdGUY-KvIEgw"); // Enter your Ad Unit ID from www.mopub.com
		// mAdView.loadAd();


	}

	protected void showToast() {
		// TODO Auto-generated method stub
		Toast.makeText(Calculator.this, "Fields Can't be empty", Toast.LENGTH_SHORT).show();
	}


	//Showing multiply results
	protected void showMultiply(float first, float second){
		{

			//float result = first* second;
			Result.setText(String.valueOf(first*second));

		}}

	protected void showDivide(float first, float second){
		{
			if (first!=0&&second!=0){

				//float result = num1 / num2;
				Result.setText(String.valueOf(first / second));
			}
			else {
				Toast.makeText(Calculator.this, "Operation not possible", Toast.LENGTH_SHORT).show();
			}
		}}	

	protected void showAdd(float first, float second){
		{
			//float result = num1 + num2;
			Result.setText(String.valueOf(first + second));
		}}

	protected void showSub(float first, float second){
		{
			//float result = num1 - num2;
			Result.setText(String.valueOf(first - second));
		}}


}
